<!--
 * @Author: 乌日汗 3337511892@qq.com
 * @Date: 2025-09-11 08:39:01
 * @LastEditors: 乌日汗 3337511892@qq.com
 * @LastEditTime: 2025-09-17 16:25:54
 * @FilePath: \大三上 前端\city\src\pages\components\Left1 copy.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
    <div>
        <Title :title="title"></Title>
        <!-- 柱状图容器 -->
        <div ref="bottom3" style="height: 108px;margin-top: 10px;"></div>
    </div>
</template>
<script>
// 引入柱状图构造函数
import { Column } from '@antv/g2plot';
// 引入get请求
import { get } from '@/utils/request';
// 引入momentjs
import moment from 'moment';
import { mapState } from 'vuex';

export default {
    data() {
        return {
            title: '实时颗粒物曲线',
            params: {
                page: 1,
                pageSize: 100
            },
            // 存放颗粒物数据
            particleData: [],
            // 柱状图实例
            column: null
        }
    },

    computed: {
        ...mapState(['ed'])
    },

    // 侦听器 侦听设备信息变化
    watch: {
        ed: {
            handler(n, o) {
                this.params.engineer_id = n.engineer_id;
                this.params.device_id = n.device_id;
                this.getParticleData();
                // 更新柱状图数据
                if (this.column) this.column.changeData(this.particleData);
            },
            deep: true
        }
    },

    methods: {
        // 加载柱状图
        loadColumn() {
            // 创建柱状图实例
            this.column = new Column(this.$refs.bottom3, {
                data: this.particleData,
                xField: 'insert_time',
                yField: 'noise',
                // x轴配置
                xAxis: {
                    label: {
                        rotate: -45,
                        style: {
                            fill: "white",
                            fontSize: 12
                        }
                    }
                },
                // y轴配置
                yAxis: {
                    label: {
                        style: {
                            fill: "white",
                            fontSize: 12
                        },
                        formatter: (value) => `${value} μg/m³`
                    }
                },
                // 柱子样式
                columnStyle: {
                    fill: '#FF6B6B',
                    stroke: '#E55934',
                    lineWidth: 1
                },
                // 柱子顶部标签
                label: {
                    style: {
                        fill: 'white',
                        fontSize: 14
                    },
                    formatter: (data) => `${data.particle_value} μg/m³`
                },
                // 交互动画
                animation: {
                    appear: {
                        animation: 'scale-in-y',
                        duration: 5000,
                    },
                },
                // 提示框配置
                tooltip: {
                    showMarkers: true,
                    domStyles: {
                        "g2-tooltip": {
                            background: '#147',
                            color: "white",
                            fontSize: 20
                        }
                    },
                    formatter: (data) => {
                        return {
                            name: '颗粒物浓度',
                            value: `${data.particle_value} μg/m³`,
                            time: data.insert_time
                        };
                    }
                },
                // 鼠标悬停效果
                state: {
                    active: {
                        style: {
                            shadowBlur: 4,
                            stroke: '#000',
                            fill: '#FF8E8E',
                        },
                    },
                },
                interactions: [{ type: 'element-active' }],
            });
            // 渲染柱状图
            this.column.render();
        },

        // 获取颗粒物数据
        async getParticleData() {
            let res = await get('/dashboard/pageQueryTodayData', this.params);
            let temp = [];

            res.data.list.forEach((item, index, arr) => {
                // 格式化时间
                item.insert_time = moment(item.insert_time).format("HH:mm");
                if (index % 10 == 0) {
                    temp.push(arr[index])
                }

            });
            this.particleData = temp;
        }
    },

    // 组件挂载时初始化
    async mounted() {
        await this.getParticleData();
        this.loadColumn();
    },

    // 组件销毁时清理
    beforeUnmount() {
        if (this.column) {
            this.column.destroy();
            this.column = null;
        }
    }
}
</script>
<style lang="less" scoped></style>